"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : vacuum
Case Name   : 验证对分区表vacuum成功
Create At   : 2023/9/26
Owner       : @li-xin12345
Description :
    1、建表并插入数据
    2、重启数据库，vacuum分区表
    3、explain查看是否走indexonlyscan
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class RowCompressTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_bug_fix_0068'

    def test_row_compress(self):
        text = '-----step1:建普通表并插入数据;expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name};
        create table {self.tb_name}(time timestamp, tag text, field int) 
        partition by range(time) interval('1 month') 
        (partition p0 values less than('2022-01-01'));
        create index on {self.tb_name}(time);
        insert into {self.tb_name} values(generate_series('2022-01-01', 
        '2022-03-31','1 hour'::interval), 'tag', generate_series(1, 1000));
        '''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS, sql_res,
                      '执行失败：' + text)
        self.assertIn('INSERT 0 2137000', sql_res, '执行失败：' + text)

        text = '-----step2:重启数据库，vacuum分区表;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.restart_db_cluster()
        self.log.info(res)
        vacuum_res = self.pri_sh.execut_db_sql(f'vacuum {self.tb_name}')
        self.log.info(vacuum_res)
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, vacuum_res,
                      '执行失败：' + text)

        text = '-----step3:explain查看是否走indexonlyscan;expect:一致-----'
        self.log.info(text)
        sql = f'explain analyze select time from {self.tb_name} order by time;'
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn('Index Only Scan', sql_res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step4:清理环境; expect:成功-----'
        self.log.info(text)
        sql = f'drop table if exists {self.tb_name};'
        drop_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_res,
                      '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')